﻿Imports System.ComponentModel

Public Class Category
    Dim _name As String
    Dim _stax As TaxClass

    Public Sub New()
        Children = New List(Of Category)
    End Sub

    Public Overridable Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Public Overridable Property DefaultTax As TaxClass
        Get
            Return _stax
        End Get
        Set(ByVal value As TaxClass)
            _stax = value
        End Set
    End Property
    Public Overridable Property Children As IList(Of Category)
    Public Overridable ReadOnly Property HasChilds As Boolean
        Get
            If Children.Count > 0 Then : Return True
            Else : Return False
            End If
        End Get
    End Property
    Public Overridable Property Parent As Category

    Public Overridable Function ToUICategory() As UICategory
        Dim temp As New UICategory
        temp.Name = Name
        temp.DefaultTax = DefaultTax
        temp.CurrentCategory = Me
        For Each item In Children
            Dim t2 As UICategory = item.ToUICategory
            t2.Parent = temp
            t2.ParentCategory = Me
            temp.Children.Add(t2)
        Next
        Return temp
    End Function

    Public Overrides Function Equals(obj As Object) As Boolean
        If obj Is GetType(Category) Then
            If CType(obj, Category).Name = Name Then
                Return True
            End If
        End If
        Return False
    End Function

    Public Overridable Property CID As String
        Get
            If Parent Is Nothing Then
                Return "-" + Name
            End If
            Return Parent.Name + "-" + Name
        End Get
        Set(value As String)

        End Set
    End Property
End Class

Public Class UICategory
    Implements INotifyPropertyChanged

    Dim _isExpanded, _isSelected As Boolean
    Dim _name As String
    Dim _stax As TaxClass
    Private _children As List(Of UICategory)
    Private _parent As UICategory

    Public Sub New()
        Children = New List(Of UICategory)
    End Sub

    Public Overridable Property IsSelected As Boolean
        Get
            Return _isSelected
        End Get
        Set(value As Boolean)
            _isSelected = value
            NotifyPropertyChanged("IsSelected")
        End Set
    End Property

    Public Overridable Property IsExpanded As Boolean
        Get
            Return _isExpanded
        End Get
        Set(value As Boolean)
            If value <> _isExpanded Then
                _isExpanded = value
                NotifyPropertyChanged("IsExpanded")
            End If

            ' Expand all the way up to the root.
            If _isExpanded AndAlso Parent IsNot Nothing Then
                Parent.IsExpanded = True
            End If
        End Set
    End Property

    Public Overridable Property Children As List(Of UICategory)
        Get
            Return _children
        End Get
        Set(value As List(Of UICategory))
            _children = value
            NotifyPropertyChanged("Children")
        End Set
    End Property

    Public Overridable Property Parent As UICategory
        Get
            Return _parent
        End Get
        Set(value As UICategory)
            _parent = value
            NotifyPropertyChanged("Parent")
        End Set
    End Property

    Public Overridable Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
            NotifyPropertyChanged("Name")
        End Set
    End Property
    Public Overridable Property DefaultTax As TaxClass
        Get
            Return _stax
        End Get
        Set(ByVal value As TaxClass)
            _stax = value
            NotifyPropertyChanged("DefaultTax")
        End Set
    End Property

    Public Overridable Property ParentCategory As Category
    Public Overridable Property CurrentCategory As Category

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class